//
//  PowerSupplyModeVC.m
//  Toyota
//
//  Created by ld on 2024/6/25.
//

#import "PowerSupplyModeVC.h"
#import "NavigateView.h"
#import "PatterySurplusRecordVC.h"
#import "LineProcessView.h"
#import "BottomSelectionBoxView.h"
#import "PopUpTipsView.h"
#import <LSTPopView.h>
#import "Definition.h"
#import "Helper.h"
@interface PowerSupplyModeVC () {
    NSString * upperLimit;   //供电上限
    UIButton * endBtn;
}
@property (nonatomic, strong)UIView * powerInfoImgView;  //充电头部图片view
@property (nonatomic, strong)UIButton * powerImgBtn;  //充电图标
@property (nonatomic, strong)UILabel * powerLab;  //充电与否文字
@property (nonatomic, strong)NavigateView * navigateView;  //导航

@property (nonatomic, strong)UIView * bottomVIew;  //下部分

@property (nonatomic, strong)LineProcessView * batterySurplusLineView;  //电量比例图
@property (nonatomic, strong)UILabel * batterySurplusLab;  //电量百分比
@end

@implementation PowerSupplyModeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColorFFFFFF;
    self.title = @"供电模式";
    upperLimit = @"";
    [self initUI];
    [self batterySurplusView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)initUI{
    self.powerInfoImgView = [[UIView alloc]init];
    [self.view addSubview:self.powerInfoImgView];
    self.powerInfoImgView.backgroundColor = kColorFFFFFF;
    [self.powerInfoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(CYScalXH(380));
    }];
    
    self.navigateView = [[NavigateView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height_StatusBar + 44) andTitleStr:@"供电管理" andRightStr:@"供电履历" andLeftBlock:^(BOOL hasBlock) {
        [self.navigationController popViewControllerAnimated:YES];
    } andRightBlock:^(BOOL hasBlock) {
        CYLog(@"供电履历");
        PatterySurplusRecordVC * vc = [[PatterySurplusRecordVC alloc]init];
        [self.navigationController pushViewController:vc animated:NO];
    }];
    [self.view addSubview:self.navigateView];
    
    UIImageView * bgImgView = [[UIImageView alloc]init];
    bgImgView.image = [UIImage imageNamed:@"powerTopImg"];
    [_powerInfoImgView addSubview:bgImgView];
    [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    _powerImgBtn  = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_powerImgBtn setBackgroundImage:[UIImage imageNamed:@"powerSupplyImg"] forState:(UIControlStateNormal)];
    [_powerImgBtn setBackgroundImage:[UIImage imageNamed:@"powerNoSupply"]forState:UIControlStateSelected];
    [_powerInfoImgView addSubview:_powerImgBtn];
    
    [_powerImgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.powerInfoImgView);
        make.size.mas_equalTo(CGSizeMake(CYScalXH(154 * 0.65), CYScalXH(154)));
        make.bottom.mas_equalTo(CYScalXH(-92));
    }];
    
    self.powerLab = [[UILabel alloc]init];
    self.powerLab.text = @"爱车供电中";
    self.powerLab.textColor = kColor141414;
    self.powerLab.font = fontBold(16);
    self.powerLab.textAlignment = NSTextAlignmentCenter;
    [_powerInfoImgView addSubview:self.powerLab];
    [self.powerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.powerInfoImgView);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(CYScalXH(-30));
    }];
    
    
}

//电池余量
- (void)batterySurplusView {
    self.bottomVIew = [[UIView alloc]init];
    [self.view addSubview:self.bottomVIew];
    
    [self.bottomVIew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(_powerInfoImgView.mas_bottom);
    }];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(20, _powerInfoImgView.customBottom + CYScalXH(30), 100, 20)];
    titleLab.text = @"电池余量:";
    titleLab.textColor = kColor141414;
    titleLab.font = fontSize(15);
    [self.bottomVIew addSubview:titleLab];
    
    _batterySurplusLineView = [[LineProcessView alloc]initWithFrame:CGRectMake(20, titleLab.customBottom + CYScalXH(25), ScreenWidth - 95, 15)];
    _batterySurplusLineView.processValue = 0.5;
    _batterySurplusLineView.defaultColor = [UIColor colorWithHex:0xF6F6F6];
    _batterySurplusLineView.fillColor = [UIColor colorWithHex:0x5EBA66];
    [self.bottomVIew addSubview:_batterySurplusLineView];
    
    _batterySurplusLab = [[UILabel alloc]initWithFrame:CGRectMake(_batterySurplusLineView.customRight , titleLab.customBottom + CYScalXH(20), 60, 25)];
    _batterySurplusLab.text = @"50%";
    _batterySurplusLab.textColor = kColor141414;
    _batterySurplusLab.font = fontSize(16);
    _batterySurplusLab.textAlignment = NSTextAlignmentCenter;
    [self.bottomVIew addSubview:_batterySurplusLab];
    
    NSArray * btnArr = @[@"结束供电", @"供电设置"];
    NSArray * btnColorArr = @[[UIColor whiteColor], kColor141414];
    for (int i = 0; i < btnArr.count; i++) {
        UIButton * choiceBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        choiceBtn.frame = CGRectMake(35, _batterySurplusLab.customBottom + CYScalXH(50) + 55 * i, ScreenWidth - 70, 40);
        [choiceBtn setTitle:btnArr[i] forState:(UIControlStateNormal)];
        if (i == 0) {
            endBtn = choiceBtn;
            [choiceBtn setBackgroundImage:[UIImage imageWithColor:kColor141414] forState:(UIControlStateNormal)];
            [choiceBtn setBackgroundImage:[UIImage imageWithColor:kColorC2C2C2] forState:(UIControlStateSelected)];
            [choiceBtn rounded:20];
        }else {
            [choiceBtn setBackgroundImage:[UIImage imageWithColor:kColorFFFFFF] forState:(UIControlStateNormal)];
            [choiceBtn rounded:20 width:1 color:kColor141414];
        }
        [choiceBtn setTitleColor:btnColorArr[i] forState:(UIControlStateNormal)];
        choiceBtn.titleLabel.font = fontSize(16);
        [choiceBtn addTarget:self action:@selector(choiceBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_bottomVIew addSubview:choiceBtn];
    }
}

- (void)choiceBtnAction:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"结束供电"]) {
        if (sender.selected) {
            [CYPreviewTip showToast:@"爱车未供电，请前往车端开始给电"];
        }else {
            [self popUpConfirmView];
            
        }
    }else  if ([sender.titleLabel.text isEqualToString:@"供电设置"]) {
        [self powerSupplySettings];
    }
}

//结束供电
- (void)popUpConfirmView {
    [LSTPopView removeAllPopView];
    PopUpTipsView * popUpView = [[PopUpTipsView alloc]initWithFrame:CGRectMake(0, 0, CYScalW(280), 200)];
    [popUpView tipsWithTitle:@"结束供电" andContent:@"爱车正在供电中，确认结束供电?" andContentAlignment:NSTextAlignmentCenter andBtnTitleArr:@[@"取消", @"结束"] andbtnColorArr:@[kColor141414, [UIColor whiteColor]] andBtnBgColorArr:@[[UIColor whiteColor], kColor141414]];
    LSTPopView *popView = [LSTPopView initWithCustomView:popUpView popStyle:LSTPopStyleFade dismissStyle:LSTDismissStyleFade];
    __block LSTPopView *blockFooter = popView;
    popView.popDuration = 0.5;
    popView.hemStyle = LSTHemStyleCenter;
    popView.dismissDuration = 0.5;
    popView.bgAlpha = 0.5;
    [popView pop];
    popView.bgClickBlock = ^{
        [blockFooter dismiss];
    };
    popUpView.customHeight = popUpView.bgV.customBottom;
    popUpView.choicActionBlock = ^(NSString * _Nonnull choiceAction) {
        if ([choiceAction isEqualToString:@"取消"]) {
            [blockFooter dismiss];
        }else if ([choiceAction isEqualToString:@"结束"]) {
            [blockFooter dismiss];
            self->endBtn.selected = YES;
            self.batterySurplusLineView.processValue = 0.2;
            self.batterySurplusLineView.fillColor = [UIColor colorWithHex:0xFF0B0A];
            self.batterySurplusLab.text = @"20%";
            self.powerImgBtn.selected = YES;
            self.powerLab.text = @"爱车未供电";
        }
    };
}

//供电设置
-(void)powerSupplySettings {
    [LSTPopView removeAllPopView];
    BottomSelectionBoxView * view = [[BottomSelectionBoxView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, CYScalXH(420)) andTitleStr:@"供电上限设置" andMsgStr:@"达到供电上限后，将结束供电" andCellArray:@[@"50%", @"40%", @"30%", @"20%", @"10%"] andIsModel:NO andChoiceArr:@[upperLimit] hasMulti:NO];
    view.customHeight = view.bgView.customHeight;
    view.backgroundColor = [UIColor whiteColor];
    [view round:10 RectCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)];
    LSTPopView *popView = [LSTPopView initWithCustomView:view popStyle:LSTPopStyleFade dismissStyle:LSTDismissStyleFade];
    __block LSTPopView *blockFooter = popView;
    popView.popDuration = 0.5;
    popView.hemStyle = LSTHemStyleBottom;
    popView.dismissDuration = 0.5;
    popView.bgAlpha = 0.5;
    [popView pop];
    view.closeBtnClick = ^{
        [blockFooter dismiss];
    };
    view.choiceBtnClick = ^(NSString * _Nonnull btnStr) {
        self->upperLimit = btnStr;
        [blockFooter dismiss];
    };
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
